package com.wqh.timerjob.service;

import com.baosight.hdsdk.HDDataProvider;
import com.baosight.hdsdk.HDTagManager;
import com.baosight.hdsdk.common.HDBasicTagPropMask;
import com.baosight.hdsdk.common.HDTagClass;
import com.baosight.hdsdk.common.HDTagDataType;
import com.baosight.hdsdk.domain.data.HDBasicTag;
import com.baosight.hdsdk.domain.data.HDDataConnection;
import com.baosight.hdsdk.domain.data.HDRecord;
import com.baosight.hdsdk.exception.HDSdkException;
import com.wqh.timerjob.schedule.JobSchedule;
import com.wqh.timerjob.util.HDManager;
import com.zeroc.Ice.Current;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


public class JobScheduleImpl implements JobSchedule {

//    public static void main(String[] args) {
//        new JobScheduleImpl().queryBasicTagAllJob("WuQiHang_basicTag_records",new Current());
//    }


    @Override
    public void addBasicTagJob(String tag, long mask, long extmask, Current current) {
        HDDataConnection con = HDManager.getConnection();
        HDTagManager tm = con.getTagManager();
        HDBasicTag basicTag = new HDBasicTag();


        //设置Tag必要属性
        String tagName="WuQiHang_basicTag_records2";
        basicTag.setName(tagName);

        basicTag.setTagClass(HDTagClass.BASIC);

        basicTag.setTagDataType(HDTagDataType.INT32);

        try {
            tm.addBasicTag(basicTag, mask,0);

            //逻辑判断：无异常输出控制台
            System.out.println("已经添加了一个普通tag，准备添加数据");

        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    @Override
    public int updateBasicTagJob(String tagStr, long mask, long extmask, Current current) {
        HDDataConnection con = HDManager.getConnection();
        HDTagManager tm = con.getTagManager();

        HDBasicTag tag ;
        String tagName="WuQiHang_basicTag_records2";
        try {
            tag = tm.getBasicTagByName(tagName);

            tag.setDesc("修改的测试用例");
            //获取修改的属性对应的属性掩码，标明该属性被修改
            mask = HDBasicTagPropMask.TAG_PROP_DESCRIPTOR;

            tm.modifyBasicTag(tag, mask, 0);
            System.out.println("修改成功！");
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return -1;
    }

    @Override
    public int removeBasicTagJob(String name, Current current) {
        HDDataConnection con = HDManager.getConnection();
        HDTagManager tm = con.getTagManager();

        HDBasicTag tag ;
        try {
            tag = tm.getBasicTagByName(name);

            if(tag != null){
                tm.deleteBasicTagByName(name);
                return 1;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public String queryBasicTagJob(String name, Current current) {
        HDDataConnection con = HDManager.getConnection();
        HDDataProvider provider  = new HDDataProvider(con);
        HDTagManager tagMgr = con.getTagManager();
        HDRecord record=null;

        try {
            HDBasicTag bTag = tagMgr.getBasicTagByName(name);
            record = provider.querySnapshotByTagID(bTag.getId());
            System.out.println(record);
        } catch (HDSdkException e) {
            e.printStackTrace();
        }


        return record.toString();
    }

    private  Date String2Date(String str) {
        java.util.Date date = null;
        // 如果是空串，或者不合适的串的话，返回null
        if (str.trim().length() == 0)// regex
        {
            return null;
        }
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            date = format.parse(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return date;
    }

    @Override
    public String queryBasicTagAllJob(String name, Current current) {

        HDDataConnection con = HDManager.getConnection();
        HDDataProvider provider  = new HDDataProvider(con);
        HDTagManager tagMgr = con.getTagManager();
        List<HDRecord> list = new ArrayList<>();
        Date startTime = this.String2Date("2020-07-28 06:00:00");

        try {
            HDBasicTag bTag = tagMgr.getBasicTagByName(name);
            list= provider.queryTagRawRecordsByCount(bTag.getId(),startTime,false,1000);

        } catch (HDSdkException e) {
            e.printStackTrace();
        }
        return list.toString();
    }
}
